import BrowserPrint from '@/utils/BrowserPrint'
export default {
    data() {
        return {
            selected_device: null,
            devices: [],
            BrowserPrint: null,
            zebraDevice:""
        }
    },
    created() {
        this.BrowserPrint = BrowserPrint()
    },
    beforeMount() {
        try {
            this.BrowserPrintsetup()
        } catch (error) {
            // alert(error)
            this.$message({
                message:'连接打印机失败!'+error,
                type:'error'
            })
        }

    },
    methods: {
        BrowserPrintsetup() {
            const _this = this;
            //Get the default device from the application as a first step. Discovery takes longer to complete.
            this.BrowserPrint.getDefaultDevice("printer", function (device) {
                //Add device to list of devices and to html select element
                console.log('初始化')
                console.log('默认设备', device)
                _this.selected_device = device;
                _this.zebraDevice = device.name
                _this.devices.push(device);
                console.log('selected_device:', _this.selected_device)
                //Discover any other devices available to the application
                _this.BrowserPrint.getLocalDevices(function (device_list) {
                    for (var i = 0; i < device_list.length; i++) {
                        //Add device to list of devices and to html select element
                        var device = device_list[i];
                        if (!_this.selected_device || device.uid != _this.selected_device.uid) {
                            _this.devices.push(device);
                        }
                    }
                    console.log('所有设备', _this.devices)
                }, function () { alert("Error getting local devices") }, "printer");
                console.log('初始化结束')
            }, function (error) {
                // alert(error);
                _this.$message({
                    message:'连接打印机失败!'+error,
                    type:'error'
                })
            })

        },
        //打印
        writeToSelectedPrinter(MaterialName, MaterialNo, BoxNo, BoxNum, qrCode) {
            const dataToWrite =
                '^XA' +
                '^CI28' +
                // '^SEE:GB18030.DAT'+
                '^CW3,E:SIMSUN.TTF' +
                '^LH0,0' +
                '^MD15' +
                '^FO100,100^A3N,48,48^FD物料名称^FS' + '^FO700,100^A3N,48,48^FD' + MaterialName + '^FS' +
                '^FO100,200^A3N,48,48^FD物料编号^FS' + '^FO700,200^A3N,48,48^FD' + MaterialNo + '^FS' +
                '^FO100,300^A3N,48,48^FD箱号^FS' + '^FO700,300^A3N,48,48^FD' + BoxNo + '^FS' +
                '^FO100,400^A3N,48,48^FD箱数^FS' + '^FO700,400^A3N,48,48^FD' + BoxNum + '^FS' +
                '^FO100,500^BQ2,2,10^FD' + qrCode + '^FS' +
                '^FO600,500^BY2^BCN,90,N^FD' + qrCode + '^FS' +
                '^FO640,600^ATN,24,24^FD' + qrCode + '^FS' +
                '^FO580,480^GB480,170,6^FS' +
                '^XZ'
            this.selected_device.send(dataToWrite, undefined, this.errorCallback);
        },
        printCustom(dataToWrite,successCallback,errorCallback) {
          //debugger;
          this.selected_device.send(dataToWrite, successCallback, errorCallback);
          console.log("打印完成")
        }
        ,
        errorCallback(errorMessage) {
            alert("Error: " + errorMessage);
        },
        //获取设备配置信息
        getConfiginjs() {
            this.BrowserPrint.getApplicationConfiguration(function (config) {
                alert(JSON.stringify(config))
            }, function (error) {
                alert(JSON.stringify(new this.BrowserPrint.ApplicationConfiguration()));
            })
        },
        zebraChange(val){
            for(let i=0;i<this.devices.length;i++){
                if(this.devices[i].name == val){
                    this.selected_device = this.devices[i]
                    this.zebraDevice = this.selected_device.name
                    break;
                }
            }
        }
    }
}
